home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8239 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Explain this> %s \"%[^\"]\"
  5. Date: 29 Feb 1996 16:59:03 GMT
  6. Organization: Los Alamos National Laboratory
  7. Distribution: world
  8. Message-ID: <TANMOY.96Feb29095903@qcd.lanl.gov>
  9. References: <4h2t6u$v56@useneta1.news.prodigy.com>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: DZYS46D@prodigy.com's message of 29 Feb 1996 00:49:34 GMT
  14.  
  15. In article <4h2t6u$v56@useneta1.news.prodigy.com>
  16. DZYS46D@prodigy.com (David Cunningham) writes:
  17. <snip>
  18. DC: 
  19. DC: I have a file that contains text like this:
  20. DC: 
  21. DC: 012345678     "Joe  Smith"
  22. DC: 
  23. DC: 
  24. DC: I also have an example of sscanf to pick out these fields. Can someone 
  25. DC: translate what  \"%[^\"]\"%d"  means. I need to have a good understanding 
  26.  
  27. Nothing. It is not even a complete token :-)
  28.  
  29. DC: of this for a homework assignment. It looks like this:
  30. DC: 
  31. DC: sscanf(buff,"%s \ "%[^\"]\" %d",student_list->ss_num,student_list->name);
  32.  
  33. Nothing. lexical error again. (And if the lexical error were
  34. corrected, there would have been a programming error: you are telling
  35. scanf to fill three locations and passing it only two things: I am not
  36. even sure they have the right type. WHY do people not post a _short_
  37. complete compilable example: Steve, can the posted copy of the FAQ
  38. mention it in large bold letters: THOU SHALT NOT LITTER C.L.C. WITH
  39. ANY FRAGMENT THAT SPEWETH OFF YOUR FINGERS? A lot more bandwidth is
  40. wasted by improper discussions than by people actually posting code).
  41.  
  42. If you need to understand the hieroglyphics, first you have to learn
  43. to reproduce them :-)
  44.  
  45. Anyway, "%[^\"]\"%d" is a string literal, that is an unmodifiable
  46. static array of characters containing the following chars:
  47. '%','[','^','"',']','"','%','d' and '\0'. When sent to scanf it
  48. encodes the following instructions:
  49.  
  50. 1) Match (%) the sequence of any characters ([...]) that do not (^)
  51.    contain '"'; and put the result in whatever the first following
  52.    argument points to. Assume that the argument is a pointer to char.
  53.    (If no such character exists, stop).
  54. 2) Check that the next character is a '"'. If it is not stop. If so,
  55.    discard it.
  56. 3) Read an integer after skipping over whitespace, and store it in
  57.    whatever the next argument points to. Assume that argument is a
  58.    pointer to int.
  59.    (If an integer is not found, stop)
  60.  
  61. Return the number of things stored.
  62.  
  63. "%s \"%[^\"]\" %d" means (in less detail than above):
  64.  
  65. 1) Means read a sequence of non-whitespace characters and store it in
  66.    the pointer to char passed.
  67. 2) skip over whitespace
  68. 3) check and read a following ".
  69. 4) Read non-" characters and store them in a pointer to char that
  70.    follows. 
  71. 5) check and read a following ".
  72. 6) skip over blanks.
  73. 7) skip over blanks and read a decimal integer storing it in the
  74.    pointer to int provided.
  75.  
  76. Hope this helps
  77.  
  78. <snip>
  79. DC: Please explain what these hieroglyphics mean, character by character.
  80. DC: 
  81. DC: The sscanf will produce this--> 012345678  Joe Smith
  82. DC: 
  83.  
  84. I do not know any sense in which a sscanf shall produce
  85. anything. Could you explain?
  86.  
  87. Cheers
  88. Tanmoy
  89. --
  90. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  91. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  92. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  93. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  94. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  95. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  96.